package com.gexiaobao.pigeon.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.databinding.FragmentMainProcessBinding
import com.gexiaobao.pigeon.ui.auction.FragmentAuction
import com.gexiaobao.pigeon.ui.fragment.mall.FragmentShopHomePage
import com.gexiaobao.pigeon.ui.fragment.personal.MineShopHomePage
import com.gexiaobao.pigeon.ui.home.HomeFragment
import com.gexiaobao.pigeon.ui.race.RaceFragment
import com.gexiaobao.pigeon.viewmodel.MainViewModel
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView

/**
 * @Author      : hxw
 * @Date        : 2023/5/30 18:23
 * @Describe    :
 */
class MainFragment : BaseFragment<MainViewModel, FragmentMainProcessBinding>() {

    private val homeFragmentPage = HomeFragment()
    private val matchFragmentPage = RaceFragment()
    private val auctionFragmentPage = FragmentAuction()
    private val marketFragmentPage = FragmentShopHomePage()
    private val mineFragmentPage = MineShopHomePage()

    override fun onResume() {
        super.onResume()
        eventViewModel.toNoticeBackHome.value = true
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.mainViewpager.addOnPageChangeListener(viewPagerListener)
        mDatabind.mainBottom.itemIconTintList = null
        val fragmentManager = activity?.supportFragmentManager
        val fragmentPagerAdapter = object : FragmentPagerAdapter(fragmentManager!!) {
            override fun getCount(): Int {
                return 5
            }

            override fun getItem(position: Int): Fragment {
                when (position) {
                    0 -> return homeFragmentPage
                    1 -> return matchFragmentPage
                    2 -> return auctionFragmentPage
                    3 -> return marketFragmentPage
                    4 -> return mineFragmentPage

                }
                return homeFragmentPage
            }

        }
        mDatabind.mainViewpager.adapter = fragmentPagerAdapter
        mDatabind.mainBottom.setOnNavigationItemSelectedListener(navigationViewListener)
        setBadge()
    }

    /**
     * 给BottomNavigationView设置Badge小红点
     */
    @SuppressLint("RestrictedApi")
    private fun setBadge() {
        //获取底部菜单view
        val menuView = mDatabind.mainBottom.getChildAt(0) as BottomNavigationMenuView
        //获取第二个item
        val itemView = menuView.getChildAt(1) as BottomNavigationItemView
        //引入badgeView
        val badgeView = LayoutInflater.from(activity).inflate(R.layout.layout_badge_view, menuView, false)
        //把badgeView添加到itemView中
        itemView.addView(badgeView)
        val count = badgeView.findViewById<TextView>(R.id.tv_badge)
        //不显示则隐藏
        count.visibility = View.GONE
        val home = mDatabind.mainBottom.menu.findItem(R.id.minefragment)
        home.setIcon(R.mipmap.icon_home_page_checked)
    }

    private val viewPagerListener = object : ViewPager.OnPageChangeListener {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
        }

        override fun onPageSelected(position: Int) {
            mDatabind.mainBottom.menu.getItem(position).isChecked = true
            when (position) {
                0 -> {
                    mDatabind.mainBottom.menu.getItem(0).setIcon(R.mipmap.icon_home_page_checked)
                    mDatabind.mainBottom.menu.getItem(1).setIcon(R.mipmap.ic_race_normal)
                    mDatabind.mainBottom.menu.getItem(2).setIcon(R.mipmap.icon_auction_unpressed)
                    mDatabind.mainBottom.menu.getItem(3).setIcon(R.mipmap.ic_market_normal)
                    mDatabind.mainBottom.menu.getItem(4).setIcon(R.mipmap.ic_mine_normal)
                }

                1 -> {
                    mDatabind.mainBottom.menu.getItem(1).setIcon(R.mipmap.ic_race_checked)
                    mDatabind.mainBottom.menu.getItem(0).setIcon(R.mipmap.icon_home_page_normal)
                    mDatabind.mainBottom.menu.getItem(2).setIcon(R.mipmap.icon_auction_unpressed)
                    mDatabind.mainBottom.menu.getItem(3).setIcon(R.mipmap.ic_market_normal)
                    mDatabind.mainBottom.menu.getItem(4).setIcon(R.mipmap.ic_mine_normal)
                }

                2 -> {
                    mDatabind.mainBottom.menu.getItem(2).setIcon(R.mipmap.icon_auction_pressed)
                    mDatabind.mainBottom.menu.getItem(0).setIcon(R.mipmap.icon_home_page_normal)
                    mDatabind.mainBottom.menu.getItem(1).setIcon(R.mipmap.ic_race_normal)
                    mDatabind.mainBottom.menu.getItem(3).setIcon(R.mipmap.ic_market_normal)
                    mDatabind.mainBottom.menu.getItem(4).setIcon(R.mipmap.ic_mine_normal)
                }

                3 -> {
                    mDatabind.mainBottom.menu.getItem(3).setIcon(R.mipmap.ic_market_checked)
                    mDatabind.mainBottom.menu.getItem(0).setIcon(R.mipmap.icon_home_page_normal)
                    mDatabind.mainBottom.menu.getItem(1).setIcon(R.mipmap.ic_race_normal)
                    mDatabind.mainBottom.menu.getItem(2).setIcon(R.mipmap.icon_auction_unpressed)
                    mDatabind.mainBottom.menu.getItem(4).setIcon(R.mipmap.ic_mine_normal)
                }
                4 -> {
                    mDatabind.mainBottom.menu.getItem(4).setIcon(R.mipmap.ic_market_checked)
                    mDatabind.mainBottom.menu.getItem(0).setIcon(R.mipmap.icon_home_page_normal)
                    mDatabind.mainBottom.menu.getItem(1).setIcon(R.mipmap.ic_race_normal)
                    mDatabind.mainBottom.menu.getItem(2).setIcon(R.mipmap.icon_auction_unpressed)
                    mDatabind.mainBottom.menu.getItem(3).setIcon(R.mipmap.ic_mine_normal)
                }
            }
        }

        override fun onPageScrollStateChanged(state: Int) {

        }
    }

    private val navigationViewListener = BottomNavigationView.OnNavigationItemSelectedListener {
        resetToDefaultIcon();//重置到默认不选中图片
        mDatabind.mainViewpager.currentItem = it.order
        when (it.itemId) {
            R.id.minefragment -> {
                it.setIcon(R.mipmap.icon_home_page_checked)
                return@OnNavigationItemSelectedListener true
            }

            R.id.matchfragment -> {
                it.setIcon(R.mipmap.ic_race_checked)
                return@OnNavigationItemSelectedListener true
            }

            R.id.auctionfragment -> {
                it.setIcon(R.mipmap.icon_auction_pressed)
                return@OnNavigationItemSelectedListener true
            }

            R.id.marketfragment -> {
                it.setIcon(R.mipmap.ic_market_checked)
                return@OnNavigationItemSelectedListener true
            }
            R.id.personalfragment -> {
                it.setIcon(R.mipmap.ic_mine_checked)
                return@OnNavigationItemSelectedListener true
            }
        }
        return@OnNavigationItemSelectedListener false
    }

    private fun resetToDefaultIcon() {
        val home = mDatabind.mainBottom.menu.findItem(R.id.minefragment)
        val race = mDatabind.mainBottom.menu.findItem(R.id.matchfragment)
        val auction = mDatabind.mainBottom.menu.findItem(R.id.auctionfragment)
        val market = mDatabind.mainBottom.menu.findItem(R.id.marketfragment)
        val personal = mDatabind.mainBottom.menu.findItem(R.id.personalfragment)
        home.setIcon(R.mipmap.icon_home_page_normal)
        race.setIcon(R.mipmap.ic_race_normal)
        auction.setIcon(R.mipmap.icon_auction_unpressed)
        market.setIcon(R.mipmap.ic_market_normal)
        personal.setIcon(R.mipmap.ic_mine_normal)
    }
}